﻿using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using CsvHelper;

namespace AIC_Notification
{
    static public class TeacherDB
    {
        static public IList<Teacher> Teachers;

        static string[][] AcademicManagers = new string[][]
        {
            new string[] {"Chen Zhang", "Xin Shi", "Wendy Wen"},
            new string[] {"Karry He", "Xin Shi", "Sangji Zhuoma"},
            new string[] {"Karry He", "Wai Cheng"},
            new string[] {"Karry He", "Wai Cheng"},
            new string[] {"Johnny Lee"},
            new string[] {"Xin Shi"}, 
            new string[] {"Karry He", "Wai Cheng", "Harry Zhang"}
        };

        static string[] SystemAdmins = new string[] {"mona.mo@jq-edu.com", "wai.cheng@jq-edu.com", "karry.he@jq-edu.com"};

        static public bool Initialize()
        {
            // Load teachers information from CSV from Salesforce
            try
            {
                using (StreamReader sr = new StreamReader(Global.InputDirectory + Global.config.TeacherFileName))
                {
                    //Csv reader reads the stream
                    CsvReader csvread = new CsvReader(sr);
                    csvread.Configuration.RegisterClassMap<TeacherMap>();
                    csvread.Configuration.HeaderValidated = null;

                    //csvread will fetch all record in one go to the IEnumerable object record
                    Teachers = csvread.GetRecords<Teacher>().ToList();

                    sr.Close();
                }
            }
            catch
            {
                Global.SysUtil.Log(String.Format("Failed to read from teacher file: {0}", Global.config.TeacherFileName), 2);
                return false;
            }

            return true;
        }

        static public Teacher GetTeacherByName(string Name)
        {
            return Teachers.SingleOrDefault(Teacher => Teacher.Name.Equals(Name));
        }

        static public string GetTeacherEmailByName(string Name)
        {
            try
            {
                Teacher t = Teachers.SingleOrDefault(Teacher => Teacher.Name.Equals(Name));
                return t != null ? t.Email : "";
            }
            catch
            {
                return "";
            }
        }

        static public string[] GetAcademicManager(CityDB.CityEnum ServiceCity)
        {
            return AcademicManagers[(int)ServiceCity];
        }

        static public string[] GetSystemAdmin()
        {
            return SystemAdmins;
        }
    }
}
